﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using eeenno.ReaderEngine.Utilities;

namespace eeenno.SpeeedReaderUI.Forms
{
    public partial class SettingsStandard : Form
    {
        public SettingsStandard()
        {
            InitializeComponent();
            LoadSettings();
        }

        public void LoadSettings()
        {
            bool refreshing = Settings.refresh_duration > 0;
            checkBox_auto_refresh.Checked = refreshing;
            textBox_auto_update.Enabled = refreshing;
            label_auto_refresh.Enabled = refreshing;

            checkBox_show_reading_list.Checked = Settings.show_reading_list_at_startup;
            checkBox_auto_login.Checked = Settings.auto_login;
            checkBox_hide_read_items.Checked = Settings.download_read_articles;
            textBox_page_size.Text = Settings.page_size.ToString();
            textBox_download_size.Text = Settings.download_size.ToString();
            textBox_auto_update.Text = Settings.refresh_duration.ToString();
            checkBox_enable_transitions.Checked = Settings.enable_transitions;
            checkBox_default_browser.Checked = Settings.always_open_in_browser;
            checkBox_download_images.Checked = Settings.download_images;
            checkBox_use_tiny_url.Checked = Settings.use_tiny_url;
            textBox_twitter_username.Text = Settings.twitter_username;
            textBox_twitter_password.Text = Settings.twitter_password;
        }

        private void menuItem_save_Click(object sender, EventArgs e)
        {
            Settings.show_reading_list_at_startup = checkBox_show_reading_list.Checked;
            Settings.auto_login = checkBox_auto_login.Checked;
            Settings.download_read_articles = checkBox_hide_read_items.Checked;           
            Settings.enable_transitions = checkBox_enable_transitions.Checked;
            Settings.always_open_in_browser = checkBox_default_browser.Checked;
            Settings.twitter_username = textBox_twitter_username.Text;
            Settings.twitter_password = textBox_twitter_password.Text;
            Settings.download_images = checkBox_download_images.Checked;
            Settings.use_tiny_url = checkBox_use_tiny_url.Checked;
            try
            {
                Settings.page_size = Convert.ToInt16(textBox_page_size.Text);
                Settings.download_size = Convert.ToInt16(textBox_download_size.Text);
                if (checkBox_auto_refresh.Checked)
                {
                    Settings.refresh_duration = Convert.ToInt32(textBox_auto_update.Text);
                }
                else
                {
                    Settings.refresh_duration = 0;
                }
                Settings.WriteSettings();
                DialogResult = DialogResult.OK;
            }
            catch (FormatException er)
            {
                MessageBox.Show("Please enter numeric values only for Download Size, Page Size, & Refresh Duration");
            }            
        }

        private void menuItem_cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void checkBox_auto_refresh_CheckStateChanged(object sender, EventArgs e)
        {
            bool refreshing = checkBox_auto_refresh.Checked;
            textBox_auto_update.Enabled = refreshing;
            label_auto_refresh.Enabled = refreshing;
        }
    }
}